package com.app.calculate;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.apache.commons.*;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.apache.commons.lang3.StringEscapeUtils;

public class Converter {

	private FileInputStream fileInput;
	private XMLInputFactory xif;
	private XMLStreamReader reader;

	public Converter(String file) throws FileNotFoundException,
			XMLStreamException {
		fileInput = new FileInputStream(file);
		xif = XMLInputFactory.newInstance();
		try {
			reader = xif.createXMLStreamReader(fileInput);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void handleDocument() throws Exception {
		try {
			String tempString = "\n";
			String tag = "";
			byte[] tempString2 = tempString.getBytes();
			long total = 0;
			long length = 0;
			long average = 0;
			int next = 0;

			while (reader.hasNext()) {
				next = reader.next();
				if (next == XMLStreamReader.START_ELEMENT) {
					tag = reader.getLocalName();
					if (tag.equals("doc")) {
						total++;
					} else if (tag.equals("title")) {
						tempString = reader.getElementText();
						if (tempString.contains("\"")
								|| tempString.contains("<")
								|| tempString.contains(">")
								|| tempString.contains("'")
								|| tempString.contains("&")) {
							tempString = StringEscapeUtils
									.escapeXml(tempString);
						}
						tempString2 = tempString.getBytes("UTF-8");
						length += tempString2.length;
					} else if (tag.equals("abstract") || tag.equals("anchor")) {
						tempString = reader.getElementText();
						if (tempString.contains("\"")
								|| tempString.contains("<")
								|| tempString.contains(">")
								|| tempString.contains("'")
								|| tempString.contains("&")) {
							tempString = StringEscapeUtils
									.escapeXml(tempString);
						}
						tempString += "\n";
						tempString2 = tempString.getBytes("UTF-8");
						length += tempString2.length;
					}
				}
			}
			average = Math.round((double) length / total);
			System.out.println("final length of all document is:" + length);
			System.out.println("total document is: " + total);
			System.out.println("average size is: " + average);
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void miniTest() {
		try {
			String tempString = "";
			String tag = "";
			byte[] tempString2 = tempString.getBytes();
			long total = 0;
			long length = 0;
			int next = 0;
			while (reader.hasNext()) {
				next = reader.next();
				if (next == XMLStreamReader.START_ELEMENT) {
					tag = reader.getLocalName();
					if (tag.equals("doc")) {
						total++;
						if (total > 1) {
							break;
						}
					} else if (tag.equals("title")) {
						tempString = reader.getElementText();
						if (tempString.contains("\"")
								|| tempString.contains("<")
								|| tempString.contains(">")
								|| tempString.contains("'")
								|| tempString.contains("&")) {
							tempString = StringEscapeUtils
									.escapeXml(tempString);
						}
						tempString2 = tempString.getBytes("UTF-8");
						length += tempString2.length;
					} else if (tag.equals("abstract") || tag.equals("anchor")) {
						tempString = reader.getElementText();
						if (tempString.contains("\"")
								|| tempString.contains("<")
								|| tempString.contains(">")
								|| tempString.contains("'")
								|| tempString.contains("&")) {
							tempString = StringEscapeUtils
									.escapeXml(tempString);
						}
						tempString += "\n";
						tempString2 = tempString.getBytes("UTF-8");
						length += tempString2.length;
					}
				}
			}
			System.out.println("final length of all document is:" + length);
			System.out.println("total document is: " + total);
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
